import { ref } from "vue"
import store from "@/store"
import { defineStore } from "pinia"
import { IGetRoleData } from "@/api/role/types/role"
import { getRoleDataApi } from "@/api/role"
export const useAuthStore = defineStore(
  "auth",
  () => {
    const authListStore = ref<IGetRoleData[]>([])

    /** 设置权限列表 */
    const setAuthList = (value: IGetRoleData[]) => {
      authListStore.value = value
    }
    const getAuthFn = async () => {
      await getRoleDataApi({
        page: 1,
        size: 100
      }).then((res: any) => {
        setAuthList(res.data.roleList)
      })
    }
    /**actions */
    // {
    //   //权限列表持久化
    //   getRoleDataApi({
    //     page: 1,
    //     size: 100
    //   }).then((res: any) => {
    //     setAuthList(res.data.roleList)
    //   })
    // }
    /**actions END */
    return {
      authListStore,
      setAuthList,
      getAuthFn
    }
  },
  {
    persist: {
      enabled: true,
      strategies: [
        {
          storage: localStorage
          // paths: ["jobListStore", "authListStore"] //指定要长久化的字段
        }
      ]
    }
  }
)
/** 在 setup 外使用 */
export function useAuthStoreHook() {
  return useAuthStore(store)
}
